<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>回测结果 - {{ ticker }}</title>
    <style>
        body { font-family: sans-serif; margin: 2em; background: #f8f8f8; }
        table { border-collapse: collapse; width: 100%; margin-bottom: 2em; }
        th, td { border: 1px solid #ccc; padding: 8px; text-align: center; }
        th { background-color: #eee; }
        h2 { margin-top: 2em; }
        img { max-width: 100%; border: 1px solid #ccc; }
        .summary {
            background: #fff;
            padding: 1em;
            border: 1px solid #ddd;
            margin-bottom: 2em;
            max-width: 600px;
        }
        .summary-row {
            display: flex;
            padding: 6px 0;
        }
        .summary-row .label {
            flex: 0 0 160px;  /* 左列固定宽度 */
            text-align: right;
            padding-right: 1em;
            font-weight: bold;
            color: #333;
        }
        .summary-row .value {
            flex: 1;
            text-align: left;
            color: #000;
        }
    </style>
</head>
<body>
    <h1>回测结果 – {{ ticker }}</h1>

    <div class="summary">
        <div class="summary-row"><span class="label">回测区间：</span><span class="value">{{ start }} ~ {{ end }}</span></div>
        <div class="summary-row"><span class="label">初始本金：</span><span class="value">${{ cash }}</span></div>
        <div class="summary-row"><span class="label">最终权益：</span><span class="value">${{ final_eq | round(2) }}</span></div>
        <div class="summary-row"><span class="label">总交易次数：</span><span class="value">{{ trades }}</span></div>
        <div class="summary-row"><span class="label">胜率：</span><span class="value">{{ (win_rate * 100) | round(2) }}%</span></div>
        <div class="summary-row"><span class="label">盈亏比：</span><span class="value">{{ profit_factor | round(2) }}</span></div>
        <div class="summary-row"><span class="label">年化收益率：</span><span class="value">{{ (cagr * 100) | round(2) }}%</span></div>
        <div class="summary-row"><span class="label">最大回撤：</span><span class="value">{{ (maxdd * 100) | round(2) }}%</span></div>
    </div>

    <h2>权益曲线</h2>
    <img src="{{ url_for('static', filename='images/' + img_file) }}" alt="Equity Curve">

    <p><a href="{{ url_for('index') }}">返回</a></p>

    <h2>交易明细</h2>
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>买入日期</th>
                <th>买入价格</th>
                <th>买入SMA10</th>
                <th>买入SMA20</th>
                <th>买入High20</th>
                <th>买入RSI</th>
                <th>卖出日期</th>
                <th>卖出价格</th>
                <th>卖出SMA10</th>
                <th>卖出SMA20</th>
                <th>卖出High20</th>
                <th>卖出RSI</th>
                <th>股数</th>
                <th>盈亏</th>
                <th>卖出原因</th>
            </tr>
        </thead>
        <tbody>
        {% for trade in trade_log %}
          <tr>
            <td>{{ trade.ID }}</td>
            <td>{{ trade.buy_date }}</td>
            <td>{{ trade.buy_price }}</td>
            <td>{{ trade.buy_SMA10 }}</td>
            <td>{{ trade.buy_SMA20 }}</td>
            <td>{{ trade.buy_High20 }}</td>
            <td>{{ trade.buy_RSI }}</td>
            <td>{{ trade.sell_date }}</td>
            <td>{{ trade.sell_price }}</td>
            <td>{{ trade.sell_SMA10 }}</td>
            <td>{{ trade.sell_SMA20 }}</td>
            <td>{{ trade.sell_High20 }}</td>
            <td>{{ trade.sell_RSI }}</td>
            <td>{{ trade.shares }}</td>
            <td style="color:{{ 'green' if trade.Profit >= 0 else 'red' }};">
                {{ trade.Profit }}
            </td>
            <td>{{ trade.sell_reason }}</td>
          </tr>
        {% endfor %}
        </tbody>
    </table>
</body>
</html>
